package com.xjtu.weixin.controller.template;

import com.xjtu.weixin.model.entity.MpTextTemplate;
import com.xjtu.weixin.service.MpTextTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author 杨凯
 * @create 2018-09-03 14:25
 **/
@Controller
@RequestMapping("/template/text")
@Slf4j
public class TextTemplateController {



	@Autowired
	private MpTextTemplateService templateService;

	/**
	 * 文本素材列表
	 * @return
	 */
	@GetMapping
	public String list(Model model){
		//TODO 暂未分页
		final List<MpTextTemplate> textTemplateList = templateService.getTextTemplateList();
		model.addAttribute("textTemplateList",textTemplateList);
		return "admin/template/text-list";
	}

	/**
	 * 添加文本模板
	 * @return
	 */
	@PostMapping("/add")
	@ResponseBody
	public ResponseEntity add(String templateName,String templateContent){
		MpTextTemplate textTemplate = new MpTextTemplate();
		textTemplate.setTemplateName(templateName);
		textTemplate.setTemplateContent(templateContent);
		textTemplate.setCreateTime(new Date());
		textTemplate.setCreateBy("yang kai");
		templateService.addTextTemplate(textTemplate);
		return new ResponseEntity("添加成功",HttpStatus.OK);
	}
}
